package com.nokia.framework.util.web;

import com.nokia.framework.util.empty.Empty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import net.sf.json.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作是否成功的JSON对象表示。
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class PageResultData extends ResultData {

	/** 当前页码 */
	public int pageIndex;

	/** 每页行数 */
	public int pageSize;

	/** 总记录数 */
	public long totalObjects;

	public PageResultData(String stat, String message) {
		super(stat, message);
		this.setData(new ArrayList<>());
	}

	public static PageResultData success(String message, List<?> data, int index, int size, long total) {
		PageResultData result = new PageResultData(STAT_SUCCESS, message);
		if(Empty.isNotEmpty(data)){
			result.setData(data);
		}
		result.setPageIndex(index);
		result.setPageSize(size);
		result.setTotalObjects(total);
		return result;
	}

	public static JSON successToJson(String message, List<?> data, int index, int size, long total) {
		return PageResultData.success(message,data, index, size, total).toJson();
	}

	public static PageResultData failure(String message) {
		return new PageResultData(STAT_FAILURE, message);
	}

	public static JSON failureToJson(String message) {
		return PageResultData.failure(message).toJson();
	}
}
